﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SmartMathLibrary.Geometry
{
    /// <summary>
    /// This class represents a line in a three dimensional space.
    /// </summary>
    [Serializable]
    public class Line3D : GeometricObject, ILine3D
    {
        /// <summary>
        /// The start point of the line.
        /// </summary>
        private Point3D startPoint;

        /// <summary>
        /// The endpoint of the line.
        /// </summary>
        private Point3D endPoint;

        /// <summary>
        /// Initializes a new instance of the <see cref="Line2D"/> class.
        /// </summary>
        public Line3D()
        {
            this.startPoint = new Point3D();
            this.endPoint = new Point3D();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Line3D"/> class.
        /// </summary>
        /// <param name="line">The line to clone.</param>
        public Line3D(Line3D line)
        {
            if (line == (Line3D) null)
            {
                throw new ArgumentNullException("line");
            }

            this.startPoint = line.StartPoint;
            this.endPoint = line.EndPoint;
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="Line2D"/> class.
        /// </summary>
        /// <param name="startPoint">The start point of the line.</param>
        /// <param name="endPoint">The endpoint of the line.</param>
        public Line3D(Point3D startPoint, Point3D endPoint)
        {
            if (startPoint == (Point3D) null)
            {
                throw new ArgumentNullException("startPoint");
            }

            if (endPoint == (Point3D) null)
            {
                throw new ArgumentNullException("endPoint");
            }

            this.startPoint = startPoint;
            this.endPoint = endPoint;
        }

        /// <summary>
        /// Gets or sets the start point of the line.
        /// </summary>
        /// <value>The start point of the line.</value>
        public Point3D StartPoint
        {
            get { return startPoint; }
            set { startPoint = value; }
        }

        /// <summary>
        /// Gets or sets the endpoint of the line.
        /// </summary>
        /// <value>The endpoint of the line.</value>
        public Point3D EndPoint
        {
            get { return endPoint; }
            set { endPoint = value; }
        }

        /// <summary>
        /// Calculates the length of the line.
        /// </summary>
        /// <returns>The length of the line.</returns>
        public double GetLineLength()
        {
            Point3D tempuri = this.endPoint - this.startPoint;

            return Math.Sqrt(Math.Pow(tempuri.X, 2) + Math.Pow(tempuri.Y, 2) + Math.Pow(tempuri.Z, 2));
        }

        /// <summary>
        /// Implements the operator ==.
        /// </summary>
        /// <param name="a">The first line.</param>
        /// <param name="b">The second line.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator ==(Line3D a, Line3D b)
        {
            if (System.Object.ReferenceEquals(a, b))
            {
                return true;
            }

            if (((object) a == null) || ((object) b == null))
            {
                return false;
            }

            return ((a.StartPoint == b.StartPoint) && (a.EndPoint == b.EndPoint));
        }

        /// <summary>
        /// Implements the operator !=.
        /// </summary>
        /// <param name="a">The first line.</param>
        /// <param name="b">The second line.</param>
        /// <returns>The result of the operator.</returns>
        public static bool operator !=(Line3D a, Line3D b)
        {
            return !(a == b);
        }

        /// <summary>
        /// Compares the current instance of a line with another line object. 
        /// </summary>
        /// <param name="compare">The line to compare.</param>
        /// <returns>True if the position of the lines are even otherwise the method will 
        /// return false.</returns>
        public bool CompareTo(Line3D compare)
        {
            if (compare == (Line3D) null)
            {
                throw new ArgumentNullException("compare");
            }

            return this == compare;
        }

        /// <summary>
        /// Translates the line with the specified parameters.
        /// </summary>
        /// <param name="translateValue">The value with which the line has to be translated.</param>
        public void TranslateLine(double translateValue)
        {
            this.startPoint.TranslateTransformation(translateValue);
            this.endPoint.TranslateTransformation(translateValue);
        }

        /// <summary>
        /// Translates the line with the specified parameters.
        /// </summary>
        /// <param name="translateValueX">The translate value for the x position.</param>
        /// <param name="translateValueY">The translate value for the y position.</param>
        /// <param name="translateValueZ">The translate value for the z position.</param>
        public void TranslateLine(double translateValueX, double translateValueY, double translateValueZ)
        {
            this.startPoint.TranslateTransformation(translateValueX, translateValueY, translateValueZ);
            this.endPoint.TranslateTransformation(translateValueX, translateValueY, translateValueZ);
        }

        /// <summary>
        /// Scales the line with a specified factor.
        /// </summary>
        /// <param name="scaleFactor">The scale factor for x, y and z position.</param>
        public void ScaleLine(double scaleFactor)
        {
            this.startPoint.ScaleTransformation(scaleFactor);
            this.endPoint.ScaleTransformation(scaleFactor);
        }

        /// <summary>
        /// Scales the line with specified factors.
        /// </summary>
        /// <param name="scaleFactorX">The scale factor for the x position.</param>
        /// <param name="scaleFactorY">The scale factor for the y position.</param>
        /// <param name="scaleFactorZ">The scale factor for the z position.</param>
        public void ScaleLine(double scaleFactorX, double scaleFactorY, double scaleFactorZ)
        {
            this.startPoint.ScaleTransformation(scaleFactorX, scaleFactorY, scaleFactorZ);
            this.endPoint.ScaleTransformation(scaleFactorX, scaleFactorY, scaleFactorZ);
        }

        /// <summary>
        /// Determines whether the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <param name="obj">The <see cref="T:System.Object"/> to compare with the current <see cref="T:System.Object"/>.</param>
        /// <returns>
        /// true if the specified <see cref="T:System.Object"/> is equal to the current <see cref="T:System.Object"/>; otherwise, false.
        /// </returns>
        /// <exception cref="T:System.NullReferenceException">The <paramref name="obj"/> parameter is null.</exception>
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        /// <summary>
        /// Serves as a hash function for a particular type.
        /// </summary>
        /// <returns>
        /// A hash code for the current <see cref="T:System.Object"/>.
        /// </returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            return this.startPoint.ToString() + " | " + this.endPoint.ToString();
        }
    }
}